define(['jquery','bootstrap','common'], function($,bootstrap,common){
    return {
        //初始化
        init: function(options){
            //this.changeAll("input[name='checkAll']");
            //this.clickBatch(".xf-batch");
            //this.clickWeight(".xf-weight");
            options.checkAll?this.changeAll(options.checkAll):'';
            options.batch?this.clickBatch(options.batch):'';
            options.weight?this.clickWeight(options.weight):'';
            options.get?this.get(options.get):'';
            options.delete?this.delete(options.delete):'';
        },
        //监听GET请求
        get: function(selector){
            $(document).on('click', selector, function(event){
                event.preventDefault();
                $.ajax({
                    type: 'get',
                    cache: false,
                    url: $(this).attr('href'),
                    //dataType: 'html',
                    timeout: 5000,
                    success: function(data,status,xhr) {
                        common.modalForm(data);
                    },
                    error: function(xhr,status,errorThrown) {
                        common.toastMini(errorThrown);
                    }
                });
            });
        },
        //监听删除事件
        delete: function(element){
            $(document).on("click", element, function() {
                if(confirm("你确定要执行这个操作吗？")){
                    $.get($(this).attr('href'), function(res, status, xhr) {
                        location.reload(true);
                    });
                }else{
                    common.toastMini('操作已取消');
                }
                return false;
            });
        },
        //全选事件
        changeAll: function(element){
            $(document).on('change', element, function() {
                $("input[name='id[]']").prop('checked', $(this).prop('checked'));
            });
        },
        //批量操作
        clickBatch: function(element){
            $(document).on('click', element, function(event) {
                event.preventDefault();//event.stopPropagation();
                var arrId = [];
                $("input[name='id[]']:checked").each(function(){
                    arrId.push($(this).val());
                });
                if(arrId.length===0){
                    common.toastMini('请选择ID');
                    return false;
                }
                if($(this).data('delete')){
                    if(confirm("你确定要执行这个操作吗？") != true){
                        common.toastMini('操作已取消');
                        return false;
                    }
                }
                $.ajax({
                    url: $(this).attr('href'),
                    type: 'POST',
                    data: {
                      ids: arrId
                    },
                    success: function(response) {
                      common.toastMini(response.msg);
                      common.location(response.url,500);
                    },
                    error: function(xhr,status,error) {
                      common.toastMini(error);
                    }
                });
            });
        },
        //权重值增减
        clickWeight: function(element){
            $(document).on('click', element, function() {
                $.get($(this).attr('href'), function(res, status, xhr) {
                    if(res.code == 1){
                        window.location.href = res.url;
                    }else{
                        common.toastMini(res.msg);
                    }
                });
                return false;
            });
        }
    };
});